package com.android.SummerSchoolProject;

import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.audiofx.BassBoost;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class PlayerBoard extends LinearLayout {

	public ImageButton btnPlay;
	public ImageButton btnStop;
	public ImageButton btnPause;
	public SeekBar mSeekBar;
	public BassBoost bBoost;
	public TextView clock;
	int duration;
	String minutes;
	String seconds;
	Thread seekB;

	// Context cn;
	MediaPlayer mp;

	// constructor
	public PlayerBoard(Context context, AttributeSet attr) {
		super(context, attr);
		// cn=context;
		initializeLayoutBasics(context);
		registerUIComponents(context);

	} // constuctor

	private void initializeLayoutBasics(Context context) {
		setOrientation(HORIZONTAL);
		final LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.mediaplayer, this);
	}

	private void registerUIComponents(final Context ctx) {
		clock = (TextView) findViewById(R.id.clock);
		// char lol[]={1,2,3};
		// digiClock.setText(lol, 1, 3);
		clock.setText("00:00");
		this.btnPlay = (ImageButton) findViewById(R.id.btnPlay);
		this.btnPlay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// mp.start();
				play();
			}
		});
		this.btnStop = (ImageButton) findViewById(R.id.btnStop);
		this.btnStop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				stop();
			}
		});
		this.btnPause = (ImageButton) findViewById(R.id.btnPause);
		this.btnPause.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				pause();
			}
		});

		this.mSeekBar = (SeekBar) findViewById(R.id.mSeekBar);
		this.mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				int i = mSeekBar.getProgress() / 1000;
				if (i / 60 < 10) {
					minutes = "0" + Integer.toString(i / 60);
				} else {
					minutes = Integer.toString(i / 60);
				}
				if (i % 60 < 10) {
					seconds = "0" + Integer.toString(i % 60);
				} else {
					seconds = Integer.toString(i % 60);
				}
				clock.setText(minutes + ":" + seconds);
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				pause();
			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				mp.seekTo(mSeekBar.getProgress());
				play();
			}
		});

	}
	public void setPBar(int pro){
		mSeekBar.setProgress(pro);
		mp.seekTo(pro);
	}
	
	public int getPBar(){
		return mSeekBar.getProgress();
	}

	public void setPlayer(MediaPlayer _mp) {
		// TODO Auto-generated method stub
		// mp = MediaPlayer.create(cn, R.raw.mystica);
		/*
		 * if (mp==null){ throw new Exception("player not initialized"); }
		 */

		mp = _mp;
		// bBoost = new BassBoost (0, mp.getAudioSessionId());
		// bBoost.setStrength((short) 1000);
		// bBoost.setEnabled(true);
		duration = mp.getDuration();
	}

	void play() {

		mSeekBar.setMax(duration);
		seekB = new Thread(new Runnable() {
			@Override
			public void run() {
				while (!Thread.interrupted() && mp.isPlaying()) {
					mSeekBar.setProgress(mp.getCurrentPosition());
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
				}
			}
		});
		seekB.start();

		mp.start();

		/*
		 * if (!mp.isPlaying()) {
		 * 
		 * // mp.start();
		 * 
		 * try { mp.prepare(); } catch (IOException e) { // TODO Auto-generated
		 * catch block e.printStackTrace(); } try { mp.start(); } catch
		 * (IllegalStateException e) { Log.e("EXCEPTION", e.getMessage(), e); }
		 * 
		 * } else { try { mp.pause(); } catch (IllegalStateException e) {
		 * Log.e("EXCEPTION", e.getMessage(), e); } }
		 */

	}

	void stop() {

		// seekB.interrupt();
		if (mp != null) {
			mp.seekTo(0);
			if (mSeekBar != null) {
				mSeekBar.setProgress(0);
			}
			try {
				mp.stop();
				// TODO should we release??
			} catch (IllegalStateException e) {
				Log.e("EXCEPTION", e.getMessage(), e);
			}
			try {
				mp.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	void pause() {

		if (mp != null) {
			try {
				mp.pause();
			} catch (IllegalStateException e) {
				Log.e("EXCEPTION", e.getMessage(), e);
			}
		}

	}

} // playerBoard class

